package com.sc.nft.service.impl;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.sc.nft.dao.UserEcologyCastUpperLimitDao;
import com.sc.nft.entity.UserEcologyCastUpperLimit;
import com.sc.nft.entity.UserEcologyCastUpperLimitDetails;
import com.sc.nft.enums.UserEcologyCastUpperLimitDetailsTypeEnum;
import com.sc.nft.exception.GlobalRunTimeException;
import com.sc.nft.service.UserEcologyCastUpperLimitService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Slf4j
@RequiredArgsConstructor
@Service
public class UserEcologyCastUpperLimitServiceImpl implements UserEcologyCastUpperLimitService {

    private final UserEcologyCastUpperLimitDao userEcologyCastUpperLimitDao;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addCountByUserIdAndCollectionId(Long userId, Long collectionId, Integer count, UserEcologyCastUpperLimitDetailsTypeEnum type, String eventsName, Long userRecordsId) {

        UserEcologyCastUpperLimit userEcologyCastUpperLimit = userEcologyCastUpperLimitDao.getOneByUserIdAndCollectionId(userId, collectionId);
        Integer beforeCount = 0;
        if(ObjectUtil.isNull(userEcologyCastUpperLimit)){
            UserEcologyCastUpperLimit insertUserEcologyCastUpperLimit = new UserEcologyCastUpperLimit();
            insertUserEcologyCastUpperLimit.setUserId(userId);
            insertUserEcologyCastUpperLimit.setCollectionId(collectionId);
            insertUserEcologyCastUpperLimit.setUsableCount(count);
            insertUserEcologyCastUpperLimit.insert();
        }else{
            beforeCount = userEcologyCastUpperLimit.getUsableCount();
            Assert.isTrue(
                    userEcologyCastUpperLimitDao.addCountByUserIdAndCollectionId(userId, collectionId, count),
                    () -> new GlobalRunTimeException("用户铸造上限更新失败"));
        }


        UserEcologyCastUpperLimitDetails userEcologyCastUpperLimitDetails = new UserEcologyCastUpperLimitDetails();
        userEcologyCastUpperLimitDetails.setUserId(userId);
        userEcologyCastUpperLimitDetails.setCollectionId(collectionId);
        userEcologyCastUpperLimitDetails.setUserRecordsId(userRecordsId);
        userEcologyCastUpperLimitDetails.setType(type);
        if (UserEcologyCastUpperLimitDetailsTypeEnum.EXCHANGE.equals(type)){
            userEcologyCastUpperLimitDetails.setTypeName(type.getDescription());
        }else {
            userEcologyCastUpperLimitDetails.setTypeName(StrUtil.format("{}:{}",type.getDescription(),eventsName));
        }

        userEcologyCastUpperLimitDetails.setUseCount(count);
        userEcologyCastUpperLimitDetails.setBeforeCount(beforeCount);
        userEcologyCastUpperLimitDetails.setAfterCount(beforeCount + count);
        userEcologyCastUpperLimitDetails.setIsAdd(type.getIsAdd());
        userEcologyCastUpperLimitDetails.insert();
    }

    @Override
    public void subCountByUserIdAndCollectionId(Long userId, Long collectionId, Integer count, UserEcologyCastUpperLimitDetailsTypeEnum type, String eventsName, Long userRecordsId) {
        UserEcologyCastUpperLimit userEcologyCastUpperLimit = userEcologyCastUpperLimitDao.getOneByUserIdAndCollectionId(userId, collectionId);
        Assert.notNull(userEcologyCastUpperLimit, "用户铸造上限不存在");

        Assert.isTrue(
                userEcologyCastUpperLimitDao.subCountByUserIdAndCollectionId(userId, collectionId, count),
                () -> new GlobalRunTimeException("用户铸造上限更新失败"));

        UserEcologyCastUpperLimitDetails userEcologyCastUpperLimitDetails = new UserEcologyCastUpperLimitDetails();
        userEcologyCastUpperLimitDetails.setUserId(userId);
        userEcologyCastUpperLimitDetails.setCollectionId(collectionId);
        userEcologyCastUpperLimitDetails.setUserRecordsId(userRecordsId);
        userEcologyCastUpperLimitDetails.setType(type);
        userEcologyCastUpperLimitDetails.setTypeName(type.getDescription());
        userEcologyCastUpperLimitDetails.setUseCount(count);
        userEcologyCastUpperLimitDetails.setBeforeCount(userEcologyCastUpperLimit.getUsableCount());
        userEcologyCastUpperLimitDetails.setAfterCount(userEcologyCastUpperLimit.getUsableCount() - count);
        userEcologyCastUpperLimitDetails.setIsAdd(type.getIsAdd());
        userEcologyCastUpperLimitDetails.insert();

    }

    @Override
    public UserEcologyCastUpperLimit getOne(Long userId, Long collectionId) {
        return userEcologyCastUpperLimitDao.getOneByUserIdAndCollectionId(userId, collectionId);
    }

    /**
     * 扣减用户可兑换次数
     * @param userId
     * @return
     */
    @Override
    public Boolean subExchangeCount(Long userId, Long collectionId,Integer count) {
        return userEcologyCastUpperLimitDao.subExchangeCount(userId, collectionId,count);
    }
}
